package com.weasing.res.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.weasing.res.dao.IWxAdminDao;
import com.weasing.res.dao.IWxRoleDao;
import com.weasing.res.dao.base.impl.BaseDaoImpl;
import com.weasing.res.domain.WxAdmin;
import com.weasing.res.domain.WxRole;

/***
 * 新版用的用户dao
 * 
 * @author long
 *
 */
@Repository
public class WxAdminDaoImpl extends BaseDaoImpl<WxAdmin>
		implements IWxAdminDao {
	@Autowired
	private IWxRoleDao wxRoleDao;

	/**
	 * 根据用户名和密码查询用户
	 */
	public WxAdmin findUserByUsernameAndPassword(String username,
			String password) {
		String hql = "FROM WxAdmin u WHERE u.username = ? AND u.password = ?";
		List<WxAdmin> list = (List<WxAdmin>) this.getHibernateTemplate().find(
				hql,
				username, password);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据用户名字查用户
	 * 
	 * @param 用户名
	 * 
	 */
	public WxAdmin findUserByUsername(String username) {
		String hql = "FROM WxAdmin u WHERE u.username = ?";
		List<WxAdmin> list = (List<WxAdmin>) this.getHibernateTemplate().find(
				hql,
				username);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WxAdmin> findAllAndRole() {

		List<WxAdmin> findAll = this.findAll();
		for (WxAdmin wxAdmin : findAll) {
			WxRole wxRole = wxRoleDao.findById(wxAdmin.getRoleid());
			wxAdmin.setWxRole(wxRole);
		}
		return findAll;
	}
}
